﻿using System;
using LoD.WPClientLibrary;
using LoD.WPClientLibrary.Controls;
using LoD.WPClientLibrary.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;


namespace LoD.WPClient.Modules {
    public class TitleScreen : IGameModule {
        private readonly GameClass _game;
        private ImageButton[] _buttons;
        private Label _lblTitle;
        private Sprite _mainMenu;
        private Sprite _sprBackground;

        public TitleScreen(GameClass game) {
            this._game = game;
            this.Random = new Random();
        }

        internal SpriteFont TitleFont { get; set; }
        internal SpriteFont GuiFont { get; set; }
        internal Random Random { get; set; }

        #region IGameModule Members

        public void Reset() { }

        public void LoadContent(ContentManager content) {
            this.TitleFont = content.Load<SpriteFont>("Fonts/Title");
            this.GuiFont = content.Load<SpriteFont>("Fonts/Gui");

            this._sprBackground = new Sprite(content, this._game.SpriteBatch);
            this._sprBackground.Load("Gumps/PergamentBig");
            this._sprBackground.Origin = new Vector2(0, 0);

            const string title = "Land of Dreams";
            Vector2 size = this.TitleFont.MeasureString(title);
            this._lblTitle = new Label(content, this._game.SpriteBatch, this.TitleFont) {
                Text = title,
                TextColor = Color.DarkRed,
                ShadowColor = Color.LightGray,
                UseShadow = false,
                Position = new Vector2(400 - size.X / 2, 30)
            };

            this._mainMenu = new Sprite(content, this._game.SpriteBatch);
            this._mainMenu.Load("Gumps/MainMenu");
            this._mainMenu.Position = new Vector2(400, 280);

            this._buttons = new ImageButton[6];
            this._buttons[0] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Information")
                               { Position = new Vector2(400, 150) };
            this._buttons[1] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Account")
                               { Position = new Vector2(400, 180) };
            this._buttons[2] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Intro")
                               { Position = new Vector2(220, 268) };
            this._buttons[3] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Enter")
                               { Position = new Vector2(400, 268) };
            this._buttons[4] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Setup")
                               { Position = new Vector2(580, 268) };
            this._buttons[5] = new ImageButton(content, this._game.SpriteBatch, this.GuiFont, "Gumps/Quit")
                               { Position = new Vector2(400, 358) };
        }

        public void Update(TouchLocation touch, GameTime gameTime) {
            int tapped = -1;
            int n = 0;
            foreach (ImageButton btn in this._buttons) {
                btn.Update(touch);
                btn.Animate();

                if (btn.Tapped) tapped = n;
                n++;
            }

            switch (tapped) {
                case 3:
                    this._game.GameState = GameClass.GameStates.Playing;
                    break;
                case 5:
                    this._game.Exit();
                    break;
            }
        }

        public void Draw(GameTime gameTime) {
            this._sprBackground.Draw();
            this._lblTitle.Draw();
            this._mainMenu.Draw();
            foreach (ImageButton btn in this._buttons) btn.Draw();
        }

        #endregion
    }
}